from Autotests.BaseTest import AirManagerTestCase
from Autotests.LocalMachine.Pages.AirManager.DevicePage import *
from Autotests.LocalMachine.Pages.AirManager.MainPage import *


class DeviceTable(AirManagerTestCase):
    def setUp(self):
        super(DeviceTable, self).setUp()
        self.mp = WebMainPage()

    def test_SortDevices(self):
        self.mp.sortDevices(DeviceTableHeader.device, DeviceSortMode.ascending)
        self.assertTrue(self.mp.isColumnSorted(DeviceTableHeader.device, DeviceSortMode.ascending))

        allColumns = [getattr(DeviceTableHeader, v) for v in vars(DeviceTableHeader)
                      if isinstance(getattr(DeviceTableHeader, v), DeviceHeaderItem)]
        sortModes = [DeviceSortMode.ascending, DeviceSortMode.descending]
        for column in allColumns:
            for mode in sortModes:
                self.mp.sortDevices(column, mode)
                #print('Sorting column - %s, mode - %s' % (column.id, mode))
                self.assertTrue(self.mp.isColumnSorted(column, mode),
                                'Check sorting for column - %s, mode - %s' % (column.id, mode))

    def test_DeviceInfo(self):
        device = 'cb815f0b7831'
        expectedOS = 'Windows 7 Ultimate Edition Service Pack 1 (7601), 64-bit'
        expectedDataSourceInfo = ['Files', '4.87 KB', '02/18/14 10:16 AM', 'Failed']
        expectedBackupInfo = [
            ['', 'Backup', '02/18/14 10:19 AM', 'Files', '1s', 'Failed', '0 B', '1', '1', '0'],
            ['', 'Backup', '02/18/14 10:15 AM', 'Files', '45s', 'Completed', '4.88 KB', '1', '0', '0'],
            ]

        self.mp.openDevice(device)

        overview = Overview()
        self.assertEqual(overview.getEmailInfo(), '', 'Check email info')
        self.assertEqual(overview.getOSInfo(), expectedOS, 'Check os info')
        self.assertEqual(overview.getDataSourceInfo(0), expectedDataSourceInfo, 'Check data source info')

        backupInfo = BackupInfo()
        self.assertEqual(backupInfo.getBackupInfo(0), expectedBackupInfo[0], 'Check backup info line 1')
        self.assertEqual(backupInfo.getBackupInfo(1), expectedBackupInfo[1], 'Check backup info line 2')

        statistics = Statistics()
        self.assertTrue(statistics.checkTablesIsNotEmpty(), 'Check statistics tables is not empty')
        # TODO: have to add scrolling
        # self.assertTrue(statistics.checkTableGeneralHasNoEmptyField(),
        #                 'Check table General does not contain empty cells')
        # self.assertTrue(statistics.checkTableAdvancedHasNoEmptyField(),
        #                 'Check table Advanced does not contain empty cells')
        # self.assertTrue(statistics.checkTableFilesHasNoEmptyField(),
        #                 'Check table Files does not contain empty cells')

        errorsInfo = ErrorsInfo()
        self.assertTrue(errorsInfo.getErrorsCount() > 0, 'Check "Recent errors" contains records')
        errorsInfo.cancel()

    def test_AssignDeviceToGroup(self):
        groupName = 'groupForDevice'

        deviceCount = self.mp.getDeviceCount()
        deviceList = self.mp.getColumnItems(DeviceTableHeader.device)

        if groupName in self.mp.getGroupList(): self.mp.deleteGroup(groupName)
        self.mp.addGroup(groupName)

        self.mp.selectAllDeviceView()
        self.mp.openDevice()
        DevicePage().assignGroup(groupName)
        self.assertEqual(self.mp.getDeviceCount(), deviceCount,
                         'Check device number was not changed after assignment in All device view')
        self.mp.selectGroupView(groupName)
        self.assertEqual(self.mp.getDeviceCount(), 1,
                         'Check only 1 device is in device table')
        self.assertTrue(deviceList[0] in self.mp.getColumnItems(DeviceTableHeader.device),
                        'Check correct device is shown')

        self.mp.openDevice()
        DevicePage().unassignGroup()
        self.assertEqual(self.mp.getDeviceCount(), 0,
                         'Check the device table is empty')
        self.mp.selectAllDeviceView()
        self.assertEqual(self.mp.getDeviceCount(), deviceCount,
                         'Check device number was not changed after assignment/unassignment')
        self.mp.deleteGroup(groupName)
    
    def test_SearchDevices(self):
        self._searchAndVerify(SearchFilterBy.anyField, SearchFilterType.contains, 'Something', False)
        self._searchAndVerify(SearchFilterBy.anyField, SearchFilterType.equals, 'cb815f0b7831', True)
        
        self._searchAndVerify(SearchFilterBy.deviceId, SearchFilterType.equals, 'Nothing', False)
        self._searchAndVerify(SearchFilterBy.deviceId, SearchFilterType.beginsWith, 'cb8', True)
        
        self._searchAndVerify(SearchFilterBy.name, SearchFilterType.beginsWith, 'qqq', False)
        self._searchAndVerify(SearchFilterBy.name, SearchFilterType.endsWith, 'WIN', True)
        
        self._searchAndVerify(SearchFilterBy.os, SearchFilterType.endsWith, 'X32', False)
        self._searchAndVerify(SearchFilterBy.os, SearchFilterType.contains, '64-bit', True)
        
        self._searchAndVerify(SearchFilterBy.version, SearchFilterType.contains, '13', False)
        self._searchAndVerify(SearchFilterBy.version, SearchFilterType.beginsWith, '14.0', True)
        
    def _searchAndVerify(self, filterBy, filterType, searchText, isFound):
        self.mp.search(filterBy, filterType, searchText)
        if isFound:
            self.assertTrue(self.mp.getDeviceCount() >= 1,
                            'Check devices found. FilterBy=%s , FilterType=%s, SearchText=%s' %
                            (filterBy, filterType, searchText))
        else:
            self.assertTrue(self.mp.getDeviceCount() == 0,
                            'Check devices not found. FilterBy=%s , FilterType=%s, SearchText=%s' %
                            (filterBy, filterType, searchText))


import unittest
if __name__ == "__main__":
    unittest.main()
